package com.sxj.corejava.code14_Lambda表达式;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Test04_断言型接口 {

    public static void main(String[] args) {
        List<String> citys = Arrays.asList("南京","苏州","北京","上海","常州","泰州","徐州","无锡","盐城","连云港","扬州");
        // 匿名内部类用法
        testPredicate(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                // 打印带有州字的城市
                return s.contains("州");
            }
        },citys);
        System.out.println("***********************************");
        // Lambda表达式用法,打印带有京字的城市
        testPredicate(s -> s.contains("京"),citys);
    }

    /**
     * 断言型接口 boolean test(T t);
     * @param predicate
     * @param list
     */
    public static void testPredicate(Predicate<String> predicate, List<String> list) {
        for (String s : list) {
            if (predicate.test(s)) {
                // 当断言返回值为true时打印对应数据
                System.out.println(s);
            }
        }
    }

}
